﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace weixin.api
{
    sealed public class DeserializeJson : Dictionary<string, object>
    {
        private Dictionary<string, object> _all_keyVal = new Dictionary<string, object>();

        private void GetElement(string key, object _DicData)
        {
            Dictionary<string, object> data = _DicData as Dictionary<string, object>;
            if (null != data)
            {
                foreach (var item in data)
                {
                    string _type = item.Value.GetType().Name;
                    string _newkey = key + "." + item.Key;
                    if (_type == ("Dictionary`2"))
                    {
                        GetElement(_newkey.Trim('.'), item.Value);
                    }
                    else if (_type == ("ArrayList"))
                    {
                        System.Collections.ArrayList _array = item.Value as System.Collections.ArrayList;
                        if (_array != null)
                        {
                            _all_keyVal.Add(_newkey.Trim('.') + ".length", _array.Count);
                            int ind = 0;
                            foreach (var itm in _array)
                            {
                                string __type = itm.GetType().Name;
                                if (__type == ("Dictionary`2"))
                                {
                                    GetElement((_newkey + "." + ind++).Trim('.'), itm);
                                }
                                else
                                {
                                    _all_keyVal.Add((_newkey + "." + ind++).Trim('.'), itm);
                                }
                                //Response.Write(itm+"<br>");
                            }
                        }
                    }
                    else if (_type == "String" || _type == "Int32")
                    {
                        _all_keyVal.Add(_newkey.Trim('.'), item.Value);
                        //Response.Write(item.Key + "-->" + item.Value + "<br>");
                    }
                    else
                    {
                        _all_keyVal.Add(_newkey.Trim('.'), item.Value);
                        //Response.Write(item.Key + "-->" + item.Value + "<br>");
                    }
                }
            }
        }

        public DeserializeJson GetResult(string _jsonstring)
        {
            _all_keyVal = new Dictionary<string, object>();
            _all_keyVal.Clear();
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            Dictionary<string, object> result = (Dictionary<string, object>)serializer.Deserialize<Dictionary<string, object>>(_jsonstring);
            GetElement(string.Empty, result);
            this.Clear();
            foreach (var item in _all_keyVal)
            {
                this.Add(item.Key, item.Value);
            }
            return this;
            //return _all_keyVal;
        }

        public object GetObject(string _key)
        {
            if (this.Keys.Contains(_key))
            {
                return this[_key];
            }
            else
            {
                return null;
            }
        }

        public string GetString(string _key)
        {
            if (this.Keys.Contains(_key))
            {
                return this[_key].ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        public Int32 GetInt32(string _key)
        {
            if (this.Keys.Contains(_key))
            {
                return Int32.Parse(this[_key].ToString());
            }
            else
            {
                return 0;
            }
        }

        public Int32 GetInt32(string _key, Int32 _default)
        {
            if (this.Keys.Contains(_key))
            {
                return Int32.Parse(this[_key].ToString());
            }
            else
            {
                return _default;
            }
        }

        public string Output()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("DeserializeJson result = new DeserializeJson().GetResult(str);");
            sb.Append("foreach (var item in result)");
            sb.Append("{");
            sb.Append("    Response.Write(item.Key + \"-->\" + result.GetObject(item.Key) + \"<br>\");");
            sb.Append("}");
            sb.Append("Response.Write(\"aaa-->\" + result.GetInt32(\"aaa\",1000) + \"<br>\")");
            return sb.ToString();
        }
        
    }
}
